package cn.lblbc.lib.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import cn.lblbc.lib.R

class LblRecyclerView<T>(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) {
    private var adapter: LblRvAdapter<T>
    private var rv: RecyclerView

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.lbl_view_rv, this, true)
        adapter = LblRvAdapter()
        rv = view.findViewById(R.id.recyclerView)
        rv.adapter = adapter
        rv.layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)
    }

    fun setColumns(count: Int) {
        rv.layoutManager = StaggeredGridLayoutManager(count, StaggeredGridLayoutManager.VERTICAL)
    }

    fun setLayoutResId(getLayoutResId: () -> Int) {
        adapter.getLayoutResId = getLayoutResId
    }

    fun setOnBind(onBind: (itemView: View, data: T) -> Unit) {
        adapter.onBind = onBind
    }

    fun setOnItemClick(onItemClick: (data: T) -> Unit) {
        adapter.onItemClick = onItemClick
    }

    fun setData(goodsList: List<T>) {
        adapter.setData(goodsList)
    }
}